package com.xing.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyInterceptor implements HandlerInterceptor {

    @Override
    //在请求处理的方法之前执行
    //如果返回true执行下一个拦截器
    //如果返回false就不执行下一个拦截器
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        //如果username有值，说明登录了，放行
       if(session.getAttribute("LoginName")!=null){
           return true;
       }
       //如果请求包含login说明它是去登录，放行
       if(request.getRequestURI().contains("login")){
           return true;
       }
       if(request.getRequestURI().contains("username")){
           return true;
       }

       request.setAttribute("msg","请先登录！！！");
       //如果没登录跳转到登录页面
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);

       return false;
    }



}
